package cn.lql.org.controller;

// @Controller
// @ResponseBody

import cn.lql.basic.util.AjaxResult;
import cn.lql.basic.util.PageList;
import cn.lql.org.domain.Employee;
import cn.lql.org.query.EmployeeQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
// @GetMapping("/id")
// @PostMapping
// @DeleteMapping
// @PutMapping
// @PatchMapping
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @AutowiredeeService employeeService;

    private IEmploy
    // 新增或修改员工 put
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.add(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }

    }

    // 删除员工 delete
    @DeleteMapping("/{id}")  // http://localhost:8080/dept/1
    public AjaxResult deleteById(@PathVariable("id") Long id){

//           try {
//             employeeService.delete(id);
//             return new AjaxResult();
//         } catch (Exception e) {
//             e.printStackTrace();
//             AjaxResult result = new AjaxResult();
//             result.setSuccess(false);
//             result.setMessage("操作失败！");
//             return result;
//         }

        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }

    }

    // 查询一个员工 get
    @GetMapping("/{id}")  // http://localhost:8080/dept/1
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    // 获取所有员工 get
    @GetMapping // http:// localhost:8080/dept
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    // 分页和高级查询 post
    @PostMapping // http:// localhost:8080/dept
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }


    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }

    }

}
